package com.pussinboots.morning.cms.modules.goods.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.pussinboots.morning.cms.common.security.AuthorizingUser;
import com.pussinboots.morning.cms.common.util.ResultBean;
import com.pussinboots.morning.cms.common.util.SingletonLoginUtils;
import com.pussinboots.morning.cms.modules.administrator.service.IUserService;
import com.pussinboots.morning.cms.modules.system.category.entity.OsCategory;
import com.pussinboots.morning.cms.modules.system.category.service.IOsCategoryService;
import com.pussinboots.morning.cms.modules.system.product.service.IOsProductAttributeService;
import com.pussinboots.morning.cms.modules.system.product.service.IOsProductCategoryService;
import com.pussinboots.morning.cms.modules.system.product.service.IOsProductDetailService;
import com.pussinboots.morning.cms.modules.system.product.service.IOsProductImageService;
import com.pussinboots.morning.cms.modules.system.product.service.IOsProductParameterService;
import com.pussinboots.morning.cms.modules.system.product.service.IOsProductService;
import com.pussinboots.morning.cms.modules.system.product.service.IOsProductSpecificationService;
import com.pussinboots.morning.cms.modules.system.version.dto.VersionLogDTO;
import com.pussinboots.morning.cms.modules.system.version.service.IVersionLogService;
import com.pussinboots.morning.common.controller.BaseController;
import com.pussinboots.morning.common.model.PageInfo;

/**
 * 
* 项目名称：morning-cms-webapp   
* 类名称：SystemVersionLogControlller   
* 类描述：版本日至表示层控制器    
* 创建人：陈星星   
* 创建时间：2017年2月17日 上午12:32:07
 */
@Controller
@RequestMapping(value = "/system/goods")
public class GoodsClassifyControlller extends BaseController {
	
	
	/** 版本日志 */
	private static final String GOODS_CLASSIFY_LIST = getViewPath("modules/goods/system_goods_classify");
	
	/** 版本日志 */
	private static final String GOODS_CLASSIFY_SET_CATEGORY = getViewPath("modules/goods/system_goods_classify_set_category");
	
	/** 版本日志 */
	private static final String SYSTEM_VERSION_LIST = getViewPath("modules/version/system_version_list");
	
	/** 分页日志 */
	private static final String SYSTEM_VERSION_PAGE = getViewPath("modules/version/system_version_page");
	
	
	@Autowired
	private IUserService userService;
	
	@Autowired
	private IOsCategoryService categoryService;
	
	@Autowired
	private IOsProductService productService;
	@Autowired
	private IOsProductImageService productImageService;
	@Autowired
	private IOsProductDetailService productDetailService;
	@Autowired
	private IOsProductCategoryService productCategoryService;
	@Autowired
	private IOsProductParameterService productParameterService;
	@Autowired
	private IOsProductAttributeService productAttributeService;
	@Autowired
	private IOsProductSpecificationService productSpecificationService;
	@Autowired
	private IVersionLogService versionLogService;
	
	/**
	 * GET 版本日志
	 * @param model
	 * @return
	 */
	@GetMapping(value = { "", "/classify" })
	public String list(Model model) {
		return GOODS_CLASSIFY_LIST;
	}
	
	
	/**
	 * POST 版本日志
	 * @param model 
	 * @param pageInfo 分页条件
	 * @return
	 */
//	@RequiresPermissions("system:goods:classify")
//	@PostMapping(value = {"/classify" })
	@PostMapping(value = {"/classify/data" },produces=MediaType.APPLICATION_JSON_UTF8_VALUE)
	@ResponseBody
	public PageInfo list(Model model, @RequestBody JSONObject jsonParam) {
		//获取查询下条件
		Integer limit = 0;
		Integer offset = 0;
		String sortStr = "createTime";
		String order ="desc";
		
		if(jsonParam.containsKey("offset")){
			offset = jsonParam.getInteger("offset");
		}
		
		if(jsonParam.containsKey("limit")){
			limit = jsonParam.getInteger("limit");
		}
		
		if(jsonParam.containsKey("sort")){
			sortStr = jsonParam.getString("sort")!=null && !jsonParam.getString("sort").equals("") ? jsonParam.getString("sort") : sortStr;
		}
		
		if(jsonParam.containsKey("order")){
			order = jsonParam.getString("order");
		}
		
		//设置查询条件
		PageInfo pageInfo = new PageInfo();
		pageInfo.initPageSet(offset, limit, sortStr, order);
		
		//查询数据
		//pageInfo = productCategoryService.selectByPullPage(pageInfo);
		pageInfo = categoryService.selectByPullPage(pageInfo);
		
		return pageInfo;
	}
	
	
	/**
	 * 跳转道添加商品页面
	 * @return
	 */
	//@RequiresPermissions("system:goods:list:add")
	@GetMapping(value = "/classify/add")
	public String create(Model model) {
		
		//获取全部分类
		List<OsCategory> allCategory = categoryService.selectByMap(null);
		model.addAttribute("category", allCategory);
		
		return GOODS_CLASSIFY_SET_CATEGORY;
	}
	
	/**
	 * POST 版本日志
	 * @param model 
	 * @param pageInfo 分页条件
	 * @return
	 */
	//@RequiresPermissions("system:goods:classify:add")
	@ResponseBody
	@PostMapping(value = {"/classify/add" },produces=MediaType.APPLICATION_JSON_UTF8_VALUE)
	public ResultBean add(Model model, @RequestBody JSONObject jsonParam) {
		String userName = null;
		
		ResultBean result = new ResultBean();
		
		AuthorizingUser authorizingUser = SingletonLoginUtils.getUser();
		if(authorizingUser!=null){
			userName = authorizingUser.getLoginName();
		}else{
			result.setCode(0);
			result.setSuccess(false);
			result.setMessage("未登录或登陆超时，请重新登陆");
			return result;
		}
		
		if(!jsonParam.isEmpty()){
			Date dateTime = new Date();
			
			OsCategory category = jsonParam.toJavaObject(OsCategory.class);
			category.setCreateBy(userName);
			category.setCreateTime(dateTime);
			category.setUpdateBy(userName);
			category.setUpdateTime(dateTime);
			
			categoryService.insert(category);
			
			result.setCode(0);
			result.setSuccess(true);
			result.setMessage("未登录或登陆超时，请重新登陆");
			
		}else{
			result.setCode(0);
			result.setSuccess(false);
			result.setMessage("未登录或登陆超时，请重新登陆");
		}
		
		return result;
	}
	
	/**
	 * POST 版本日志
	 * @param model 
	 * @param pageInfo 分页条件
	 * @return
	 */
	@RequiresPermissions("system:goods:classify:view")
	@PostMapping(value = {"/classify/view" })
	public String view(Model model, PageInfo pageInfo) {
		VersionLogDTO versionLogDTO = versionLogService.selectByPullPage(pageInfo);
		model.addAttribute("versionLogs",versionLogDTO.getVersionLogs());
		model.addAttribute("pageInfo", versionLogDTO.getPageInfo());
		return SYSTEM_VERSION_PAGE;
	}
	
	/**
	 * 打开商品编辑页面
	 * @param model
	 * @param productId
	 * @return
	 */
	//@RequiresPermissions("system:goods:classify:edit")
	@GetMapping(value = {"/classify/edit/{id}" })
	public String edit(Model model,@PathVariable("id") Long categoryId ) {
		
		OsCategory category = categoryService.selectById(categoryId);
		model.addAttribute("oScategory",category);
		
		//获取全部分类
		List<OsCategory> allCategory = categoryService.selectByMap(null);
		model.addAttribute("category", allCategory);
		
		return GOODS_CLASSIFY_SET_CATEGORY;
	}
	
	
	/**
	 * POST 版本日志
	 * @param model 
	 * @param pageInfo 分页条件
	 * @return
	 */
	//@RequiresPermissions("system:goods:classify:delete")
	@ResponseBody
	@DeleteMapping(value = "/classify/delete/{id}")
	public ResultBean delete(Model model ,@PathVariable("id") Long categoryId ) {
		ResultBean result = new ResultBean();
		if(categoryId!=null && !categoryId.equals("")){
			try {
				Map<String,Object> map = new HashMap<String,Object>();
				map.put("category_id", categoryId);
				
				categoryService.deleteById(categoryId);
				/*productImageService.deleteByMap(map);
				productDetailService.deleteByMap(map);
				productCategoryService.deleteByMap(map);
				productParameterService.deleteByMap(map);
				productAttributeService.deleteByMap(map);
				productSpecificationService.deleteByMap(map);*/
				
				result.setCode(1);
				result.setSuccess(true);
				result.setMessage("已删除");
			} catch (Exception e) {
				e.printStackTrace();
				result.setCode(0);
				result.setSuccess(false);
				result.setMessage("已删除");
			}
		}else{
			result.setCode(1);
			result.setSuccess(true);
			result.setMessage("已删除");
		}
		
		return result;
	}
	
	
	
	
}
